package cn.edu.jdbc;

import cn.edu.jdbc.util.JdbcUtil;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 查每个班级名以及对应的班主任名字
 * SELECT c.name,t.name
 * FROM teacher t
 * JOIN class c ON t.id=c.teacher_id;
 */
public class JdbcDemo09 {
    public static void main(String[] args) {
        Connection conn = null;
        Statement st = null;
        ResultSet rs = null;
        try {
            conn = JdbcUtil.getConn();
            st = conn.createStatement();
            String sql = "SELECT c.name,t.name " +
                    "FROM teacher t " +
                    "JOIN class c ON t.id=c.teacher_id;";
            rs = st.executeQuery(sql);
            while (rs.next()){
                String cname = rs.getString("c.name");
                String tname = rs.getString("t.name");
                System.out.println(cname + "," + tname);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.close(rs, st, conn);
        }
    }
}
